<template>
  <div class="PageCart">
    <div class="cover" v-show="visible" @click="isVisible = !isVisible"></div>

    <div class="cartBar" @click="isVisible = !isVisible">
      <div class="cartIcon">
        <span class="cornerNum" v-if="cartProps.totalNum">{{cartProps.totalNum}}</span>
        <svg>
          <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-icon"></use>
        </svg>
      </div>

      <div class="cartNum">
        <div>¥ {{cartProps.totalPrice}}</div>
        <div>配送费¥{{cartProps.deliveryFee}}</div>
      </div>

      <div class="cartSubmit" :class="{enabled: cartProps.shortOrderFee === 0}" @click.stop>
        <span class="cartTips" v-if="cartProps.shortOrderFee > 0">还差¥{{cartProps.shortOrderFee}}起送</span>
        <router-link class="button" v-else :to="{path:'/confirmOrder', query:{shopId:cartProps.shopId}}">去结算</router-link>
      </div>
    </div>

    <transition name="toggleCart">
      <div class="cartList" v-show="visible">
        <header>
          <h4>购物车</h4>
          <div @click="clearCart">
            <svg>
              <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-remove"></use>
            </svg>
            <span class="clearCart">清空</span>
          </div>
        </header>
        <section class="cartListDetail">
          <ul>
            <li v-for="(item, index) in cartProps.cartFoodList" :key="index">
              <div class="cartText">
                <p class="ellipsis">{{item.name}}</p>
                <p class="ellipsis">{{item.specs}}</p>
              </div>
              <div class="cartPrice">
                <span>¥</span>
                <span>{{item.price}}</span>
              </div>
              <section class="cartControl">
                <span>
                  <svg @click="cartEvents.reduce(item.foodId, item.skuId)">
                    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-minus"></use>
                  </svg>
                </span>
                <span class="cartNum">{{item.num}}</span>
                <svg class="cartAddIcon" @click="cartEvents.add(item.foodId, item.skuId)">
                  <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-add"></use>
                </svg>
              </section>
            </li>
          </ul>
        </section>
      </div>
    </transition>
  </div>
</template>

<script>
import { mapActions } from 'vuex';
export default {
  data() {
    return {
      isVisible: false
    };
  },
  props: ['cartProps', 'cartEvents'],
  watch: {
    cartProps() {
      if (this.cartProps.totalNum === 0) {
        this.isVisible = false;
      }
    }
  },
  methods: {
    ...mapActions(['clearCart'])
  },
  computed: {
    visible() {
      return this.isVisible && this.cartProps.totalNum > 0;
    }
  }
};
</script>
<style lang="scss">
@import '../../../styles/mixin';
.PageCart {
  .cover {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.3);
    z-index: 11;
  }
  .cartBar {
    position: fixed;
    width: 100%;
    height: 2rem;
    bottom: 0;
    left: 0;
    z-index: 13;
    display: flex;
    background-color: #3d3d3f;

    .cartIcon {
      display: flex;
      background-color: #3190e8;
      position: absolute;
      padding: 0.4rem;
      border: 0.18rem solid #444;
      border-radius: 50%;
      left: 0.5rem;
      top: -0.7rem;

      .cornerNum {
        line-height: 0.8rem;
        min-width: 0.8rem;
        height: 0.8rem;
        border-radius: 0.4rem;
      }
      svg {
        width: 1.2rem;
        height: 1.2rem;
      }
    }
    .cartNum {
      color: #fff;
      margin-left: 3.2rem;
      div:nth-of-type(1) {
        font-size: 0.8rem;
        font-weight: bold;
        margin-bottom: 0.1rem;
      }
      div:nth-of-type(2) {
        font-size: 0.5rem;
      }
    }

    .cartSubmit {
      position: absolute;
      display: flex;
      right: 0;
      width: 6rem;
      height: 100%;
      text-align: center;
      align-items: center;
      justify-content: center;
      background-color: #535356;

      .cartTips {
        font-size: 0.7rem;
        color: #fff;
        font-weight: 700;
      }

      .button {
        line-height: 2rem;
        width: 100%;
        display: block;
        font-size: 0.7rem;
        color: #fff;
        font-weight: 700;
        background: #4cd964;
        height: 100%;
      }
    }
  }
  .cartList {
    position: fixed;
    width: 100%;
    bottom: 0;
    left: 0;
    padding-bottom: 2rem;
    z-index: 12;
    background-color: #fff;

    header {
      @include fj;
      align-items: center;
      padding: 0.3rem 0.6rem;
      background-color: #eceff1;
      line-height: 0.8rem;
      svg {
        @include wh(0.6rem, 0.6rem);
        vertical-align: middle;
      }
      h4 {
        @include sc(0.7rem, #666);
      }
      .clearCart {
        @include sc(0.6rem, #666);
      }
    }

    .cartListDetail {
      background-color: #fff;
      max-height: 16rem;
      overflow-y: auto;

      li {
        @include fj;
        padding: 0.4rem 0.5rem;
        position: relative;
        border-bottom: 0.04rem solid #ebebeb;

        .cartText {
          width: 55%;
          p:nth-of-type(1) {
            @include sc(0.7rem, #666);
            font-weight: bold;
          }
          p:nth-of-type(2) {
            @include sc(0.4rem, #666);
          }
        }

        .cartPrice {
          font-size: 0;
          position: absolute;
          top: 50%;
          right: 5rem;
          margin-top: -0.5rem;
          span:nth-of-type(1) {
            @include sc(0.6rem, #f60);
            font-family: Helvetica Neue, Tahoma;
          }
          span:nth-of-type(2) {
            @include sc(0.7rem, #f60);
            font-family: Helvetica Neue, Tahoma;
            font-weight: bold;
          }
        }

        .cartControl {
          display: flex;
          align-items: center;
          span {
            display: flex;
            align-items: center;
            justify-content: center;
          }
          svg {
            @include wh(0.9rem, 0.9rem);
            fill: #3190e8;
          }
          .specs_reduce_icon {
            fill: #999;
          }
          .cartNum {
            @include sc(0.65rem, #666);
            min-width: 1rem;
            text-align: center;
            font-family: Helvetica Neue, Tahoma;
          }
        }
      }
    }
  }
  .toggleCart-enter-active,
  .toggleCart-leave-active {
    transition: all 0.3s ease-out;
  }
  .toggleCart-enter,
  .toggleCart-leave-active {
    transform: translateY(100%);
  }
}
</style>